package demo4;

public class Test {

    //求每个数的平方和
    public int bitSum(int n) {
        int sum = 0;
        while (n > 0) {
            int t = n % 10;
            sum += t * t;
            n /= 10;
        }
        return sum;
    }

    public boolean isHappy(int n) {
        int slow = n, fast = bitSum(slow);
        while (slow != fast) {
            slow = bitSum(slow);
            fast = bitSum(bitSum(fast));
        }
        return slow == 1;
    }

    public int maxArea(int[] height) {
        int n = height.length;
        int left = 0, right = n - 1;
        int max = 0;
        while (left < right) {
            int h = Math.min(height[left], height[right]);
            int w = right - left;
            max = Math.max(max, h * w);
            if (height[left] < height[right]) {
                left++;
            }else {
                right--;
            }
        }
        return max;
    }

    public void duplicateZeros(int[] arr) {
        int n = arr.length, cur = 0, dest = -1;
        //1.找到复写的位置
        while (cur < n ) {
            if (arr[cur] == 0) dest += 2;
            else dest++;
            if (dest >= n - 1) break;
            cur++;
        }
        //处理特殊情况
        if (dest == n) {
            arr[n - 1] = 0;
            dest -= 2;
            cur--;
        }
        //从后向前复写
        while (cur >= 0) {
            if (arr[cur] == 0) {
                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;
            }else {
                arr[dest--] = arr[cur--];
            }
        }
    }


    public static void main(String[] args) {

    }
}
